package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.*;
import com.ipfsemc.ipfsemc.service.IOrderService;
import com.ipfsemc.ipfsemc.service.IProductService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("order")
@Api(value = "OrderContoller", tags = "订单")
public class OrderContoller {
    private static final Logger logger = LoggerFactory.getLogger(ApplyController.class);
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "购买产品生成订单", httpMethod = "POST", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResponseBase addOrder(Order order) {

        ResponseBase jsonData = new ResponseBase();
        try {

            if (null == order.getUserId() || null == order.getProId() || null == order.getOrderPrice() || null == order.getOrderState() || null == order.getOrderUp()) {
                jsonData.setMessage("参数有误");
            } else {
                int i = orderService.addOrder(order);
                if (i == -1) {
                    jsonData.setMessage("交易失败！请重新操作！");
                } else {
                    jsonData.setMessage("操作成功！！");
                    jsonData.setCode(0);
                }
            }
        } catch (Exception ex) {
            logger.error("[order][addOrder]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "购买产品生成订单", httpMethod = "POST", notes = "")
    @RequestMapping(value = "offlinePay", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单号", required = true, dataType = "String")
    })
    public ResponseBase offlinePay(String orderId) {

        ResponseBase jsonData = new ResponseBase();
        try {
            if (orderId.isEmpty() || null == orderId) {
                jsonData.setMessage("参数有误");
            } else {
                int i = orderService.offlinePay(orderId);
                if (i == -1) {
                    jsonData.setMessage("操作失败");
                } else {
                    jsonData.setMessage("操作成功！！");
                    jsonData.setCode(0);
                }
            }
        } catch (Exception ex) {
            logger.error("[order][offlinePay]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }


    @ApiOperation(value = "删除订单", httpMethod = "POST")
    @RequestMapping(value = "delOrder", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "申请ID", required = true, dataType = "String"),
    })
    public ResponseBase del(String orderId) {

        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == orderId) {
                jsonData.setMessage("参数有误");
            } else {
                int i = orderService.delOrder(orderId);
                if (i < 0) {
                    jsonData.setMessage("操作失败！！");
                } else {
                    jsonData.setMessage("操作成功！！");
                    jsonData.setCode(0);
                }
            }

        } catch (Exception ex) {
            logger.error("[order][delOrder]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    /**
     * 商品详情购买时触发的事件
     *
     * @param order
     * @return
     */
    @ApiOperation(value = "价格确认，核实", httpMethod = "GET", notes = "只看有说明的参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderPrice", value = "总价格", required = true, dataType = "String"),
            @ApiImplicitParam(name = "orderSpecs", value = "商品规格  单位T 例如：20==》20份", required = true, dataType = "String"),
            @ApiImplicitParam(name = "proId", value = "商品id", required = true, dataType = "String")
    })

    @RequestMapping(value = "surePrice", method = RequestMethod.GET)
    public ResponseBase surePrice(Order order) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == order.getOrderSpecs() || null == order.getOrderPrice() || null == order.getProId()) {
                jsonData.setMessage("参数有误");
            } else {
                Product product = productService.selectProByKey(order.getProId());
                BigDecimal price = product.getProPrice().multiply(new BigDecimal(order.getOrderSpecs()));
                if(price.compareTo(order.getOrderPrice())!=0){
                    jsonData.setMessage("计算价格有误！");
                }else{
                    jsonData.setCode(0);
                }
            }

        } catch (Exception ex) {
            logger.error("[order][surePrice]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


    //用户的订单
    @ApiOperation(value = "用户的订单", tags = "购买的会员和购买的产品订单", httpMethod = "GET")
    @RequestMapping(value = "userOrder", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String")
    })
    public JsonData listOrder(String userId) {

        JsonData jsonData = new JsonData();
        try {
            if (null == userId) {
                jsonData.setMessage("参数有误");
            } else {
                List<Order> productOrders = orderService.listOrder(userId);

                List<Order> levelOrders = orderService.listLevel(userId);

                jsonData.put("productOrders", productOrders);
                jsonData.put("levelOrders", levelOrders);
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[order][listOrder]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


    //所有订单

    @ApiOperation(value = "所有订单", httpMethod = "GET")
    @RequestMapping(value = "listOrder", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1 为产品ID 2为会员订单", required = true, dataType = "Integer")
    })
    public RespList<Order> listLevel(Integer type) {

        RespList jsonData = new RespList();
        try {
            List<Order> orders = orderService.listOrder(null);

            jsonData.setResult(orders);
            jsonData.setCode(0);

        } catch (Exception ex) {
            logger.error("[order][listOrder]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }


        return jsonData;
    }

    //后台订单详情
    @ApiOperation(value = "后台订单详情", httpMethod = "GET")
    @RequestMapping(value = "orderDetails", method = RequestMethod.GET)
    public JsonData orderDetails(String orderId) {

        JsonData jsonData = new JsonData();
        try {
            if (null == orderId) {
                jsonData.setMessage("参数有误");
            } else {
                Order order = new Order();
                order.setOrderId(orderId);
                OrderVoOne orderVoOne = orderService.selectDetailsOne(order);
                OrderVoTwo orderVoTwo = orderService.selectDetailsTwo(order);
                OrderVoThree orderVoThree = orderService.selectDetailsTree(order);

                jsonData.put("result1", orderVoOne);
                jsonData.put("result2", orderVoTwo);
                jsonData.put("result3", orderVoThree);
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[order][orderDetails]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }


        return jsonData;
    }
}
